#include "ExportFont.h"
#include "Performance.h"
#include <iostream>
#include <vector>

using namespace std;

ExportFont* ExportFont::instant;

ExportFont* ExportFont::getInstant()
{
	if ( instant == NULL )
		instant = new ExportFont();
	return instant;
}

char* ExportFont::getFNT_FileName ( char* fontSprite  )
{
	char* imageName = Util::getInstant()->getImageName ( fontSprite );
	char* fntFile = new char[strlen( imageName ) + strlen ( ".fnt" )];
	strcpy( fntFile, imageName );
	strcat( fntFile, ".fnt" );
	return fntFile;
}

void ExportFont::exportHeader ( char* fontSprite )
{
	char* fntFile = getFNT_FileName ( fontSprite );
	FILE* outF = fopen( fntFile, "w" );
	char* imageName = Util::getInstant()->getImageName( fontSprite );
	int lineHeight = Util::getInstant()->getMaxinumModuleHeight ( fontSprite );
	Point imageSize = Util::getInstant()->getImageSize ( fontSprite );
	fprintf( outF, "info face=\"%s-Thin\" size=25 bold=0 italic=0 charset=\"\" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=2,2", imageName );
	fprintf( outF, "\ncommon lineHeight=%d base=22 scaleW=%d scaleH=%d pages=1 packed=0" , lineHeight, imageSize.x, imageSize.y );
	fprintf( outF, "\npage id=0 file=\"%s\"" , Util::getInstant()->getImageNameWithExtent( fontSprite ) );	
	fclose( outF );
}

void ExportFont::exportElement ( char* fontSprite )
{
	char* fntFile = getFNT_FileName ( fontSprite );
	FILE* outF = fopen( fntFile, "a" );
	SpriteAurora* sprite = Performance::getInstant()->LoadSprite( fontSprite );
	fprintf( outF, "\nchars count=%d", sprite->numOfFrame );
	for ( int i = 0; i < sprite->numOfFrame; i++ )
	{
		//cout << endl << "id = " << i << "module " << sprite->frame[i].fModuleID;
		if ( sprite->frame[i].fModuleID < 0 )
			continue;
		int moduleID = sprite->getModuleID(i);
		char ch;
		if ( Util::getInstant()->stringContainString( sprite->module[moduleID].moduleName, "space" ) )
			ch = 32;
		else
			ch = sprite->module[moduleID].moduleName[strlen ( sprite->module[moduleID].moduleName ) - 1];
		int x = sprite->module[moduleID].moduleSize.x;
		int y = sprite->module[moduleID].moduleSize.y;
		int w = sprite->module[moduleID].moduleSize.w;
		int h = sprite->module[moduleID].moduleSize.h;
		int offX = sprite->frame[i].position.x;
		int offY = sprite->frame[i].position.y;
		fprintf( outF,"\nchar id=%d x=%d y=%d width=%d height=%d xoffset=%d yoffset=%d xadvance=%d page=0 chnl=0 letter=\"%c\"", ch, x, y, w, h, offX, offY, w, ch );
	}
		
}

void ExportFont::export_FNT_File ( char* fontSprite )
{
	exportHeader ( fontSprite );
	exportElement ( fontSprite );
}

void ExportFont::exportFont ( char* fontList )
{
	vector < Sprite_Export > spriteList = Performance::getInstant()->getFontList ( fontList );	
	for ( vector < Sprite_Export >::iterator i = spriteList.begin(); i != spriteList.end(); i++ )
	{
		export_FNT_File ( (*i).spriteName );
	}
}

void ExportFont::exportFontDefine ( char* fontList )
{
	FILE* outF = fopen ( "FontDefine.h" , "w" );
	fprintf( outF, "#ifndef _FONT_DEFINE_H" );
	fprintf( outF, "\n#define _FONT_DEFINE_H\n" );
	vector < Sprite_Export > spriteList = Performance::getInstant()->getFontList ( fontList );	
	int count = 0;
	int stringLength = 0;
	for ( vector < Sprite_Export >::iterator i = spriteList.begin(); i != spriteList.end(); i++ )
	{
		fprintf( outF, "\n#define FONT_%s %d", strupr( (*i).spriteID), count++ );
		if ( stringLength < strlen( (*i).spriteName ) )
			stringLength = strlen( (*i).spriteName );
	}

	fprintf( outF, "\n#define FONT_MAX %d", count );

	fprintf( outF, "\n\nstatic char s_fontFNTList[FONT_MAX][%d] = \n{", stringLength );
	for ( vector < Sprite_Export >::iterator i = spriteList.begin(); i != spriteList.end(); i++ )
	{
		fprintf( outF, "\n\t\"%s\",", getFNT_FileName( (*i).spriteName ) );		
	}
	fprintf( outF, "\n};" );
	fprintf( outF, "\n#endif" );
	fclose( outF );
}

void ExportFont::exportFile ( char* fontList )
{
	if ( Performance::getInstant()->fontListSuccess ( fontList ) )
	{
		exportFont( fontList );
		exportFontDefine( fontList );
	}
}